package com.lxs.doc.rocess.demo;

import ch.qos.logback.core.util.FileUtil;
import com.fasterxml.jackson.core.util.Separators;
import com.lxs.doc.rocess.common.AdvancedDocOutlineParser;
import com.lxs.doc.rocess.common.DocOutlineParser;
import com.lxs.doc.rocess.common.DocOutlineUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.http.codec.multipart.FilePart;

import javax.swing.plaf.SeparatorUI;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class DemoMain {
    public static void main(String[] args) throws Exception {
        System.out.println("File.separator = " + File.separator);
        String filePath = "D:\\360MoveData\\Users\\14046\\Desktop\\PDF_WORD.doc";
        if(filePath.endsWith(".doc")){
           processDoc(filePath);
        }else if(filePath.endsWith(".docx")){
            processDocx(filePath);
        }else{
            throw new Exception("文件格式不正确");
        }

    }

    private static void processDocx(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             XWPFDocument document = new XWPFDocument(fis)) {

            // 获取文档范围
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            Range range = (Range) paragraphs;

//            // 解析段落大纲
//            parseParagraphsOutline(range);
//
//            // 解析样式大纲
//            parseStylesOutline(document);
//
//            // 解析书签大纲（如果有）
//            parseBookmarksOutline(document);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void processDoc(String filePath) {
        // 方法1: 基础解析
        DocOutlineParser.parseDocOutline(filePath);

        // 方法2: 层次结构解析
        List<AdvancedDocOutlineParser.OutlineItem> outline =
                AdvancedDocOutlineParser.extractOutlineHierarchy(filePath);
        for (AdvancedDocOutlineParser.OutlineItem item : outline) {
            item.print("");
        }

        // 方法3: 完整信息提取
        DocOutlineUtils.DocOutlineInfo info =
                DocOutlineUtils.extractOutlineInfo(filePath);
        info.printSummary();

        // 输出详细大纲
        System.out.println("\n=== 详细大纲 ===");
        for (DocOutlineUtils.OutlineParagraph para : info.paragraphs) {
            if (para.isHeading) {
                System.out.printf("标题 L%d: %s%n", para.level, para.text);
            }
        }
    }
}
